<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Report extends CI_Controller {
	public function index(){
		$this->layout->setTitle('Báo cáo | Chi tiết');
		$month = isset($_GET['month']) ? quotes_to_entities($_GET['month']) : date('Y-m');
		$filter = " AND receive_date BETWEEN '" . $month."-01 00:00:00" . "' AND '" . $month."-31 23:59:59" . "'";
		$arrDate = explode("-", $month);
		$data['month'] = $arrDate[1].'-'.$arrDate[0];
		$smsCode = $this->session->userdata('user_sms_code');
		// get earning data
		$sqlThisMonthEarning = "SELECT sms_cost AS subtotal, 
								   DATE_FORMAT(receive_date, '%d-%m-%Y') AS earn_date
								FROM tdx_sms_stat WHERE sms_code='$smsCode' $filter";
		$monthQuery = $this->db->query($sqlThisMonthEarning);
		$data['str_date'] = 'Tháng '.$arrDate[1].'-'.$arrDate[0];
		if ($monthQuery->num_rows() > 0){
			$arrData = array();
			$total = 0;
			foreach ($monthQuery->result_array() as $row){
				$arrData[$row['earn_date']] += intval($row['subtotal']);
				$total += intval($row['subtotal']);
			}
			$data['month_data'] = $arrData;
			$data['total_month'] = $total;
		}
		else{
			$data['err'] = true;
		}
		$this->layout->view(THEME_DIR.'/report_index', $data);
	}
}